package com.hkpw.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hkpw.common.core.domain.AjaxResult;
import com.hkpw.hik.ArtemisApiPathEnum;
import com.hkpw.hik.ArtemisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.util.Objects;

/**
 * 公共
 *
 * @author yrc
 */
@Slf4j
public class BaseServiceImpl {
    /**
     * host
     */
    @Value("${hikvision.gate.host}")
    private String host;

    /**
     * appKey
     */
    @Value("${hikvision.gate.appKey}")
    private String appKey;

    /**
     * appSecret
     */
    @Value("${hikvision.gate.appSecret}")
    private String appSecret;

    /**
     * 海康接口调用公共方法
     *
     * @return
     */
    public JSONObject callPostApiCommonFun(String path, Object params) {
        log.info("[HK接口请求]callPostApiCommonFun(path={},params={})", path, params);
        JSONObject jsonResult = callApiCommonFunWithoutLog(path, params);
        return jsonResult;
    }

    /**
     * 海康接口调用公共方法
     *
     * @param path   接口地址
     * @param params 参数
     * @return
     */
    public AjaxResult callApiCommonFun(String path, Object params) {
        JSONObject jsonResult = callPostApiCommonFun(path, params);
        if (Objects.nonNull(jsonResult) && jsonResult.containsKey(ArtemisApiPathEnum.KEY_CODE.value())) {
            String code = jsonResult.getString(ArtemisApiPathEnum.KEY_CODE.value());
            if (ArtemisApiPathEnum.CODE_SUCCESS.value().equals(code)) {
                return AjaxResult.success("操作成功", jsonResult.get(ArtemisApiPathEnum.KEY_DATA.value()));
            } else {
                return AjaxResult.error(jsonResult.getString(ArtemisApiPathEnum.KEY_MSG.value()), jsonResult);
            }
        }
        return AjaxResult.error("高校安防接口调用失败:" + path);
    }

    /**
     * 不打印详细日志方法
     *
     * @param path
     * @param params
     * @return
     */
    public JSONObject callApiCommonFunWithoutLog(String path, Object params) {
        log.info("[HK接口请求]callPostApiCommonFun(path={})", path);
        return callBaseApiCommonFunWithoutLog(ArtemisUtil.ARTEMIS_PATH, path, params);

    }


    /**
     * 无参请求方法
     *
     * @param path
     * @return
     */
    public AjaxResult callApiCommonFun(String path) {
        return callApiCommonFun(path, null);
    }

    /**
     * 可传入上下文的请求方法
     *
     * @param basePath
     * @param path
     * @param params
     * @return
     */
    public JSONObject callBaseApiCommonFunWithoutLog(String basePath, String path, Object params) {
        log.info("[HK接口请求]callBaseApiCommonFunWithoutLog(basePath={},path={})", basePath, path);
        String body = null;
        if (Objects.nonNull(params)) {
            body = JSON.toJSON(params).toString();
        }
        log.info("[HK接口请求]HOST:{}", host);
        log.info("[HK接口请求]bathPath:{}", basePath);
        log.info("[HK接口请求]appKey:{}", appKey);
        log.info("[HK接口请求]appSecret:{}", appSecret);
        String result = ArtemisUtil.callPostApiCommonFun(host, basePath, appKey, appSecret, path, body);
        log.info("[HK接口请求]返回结果jsonResult={}", result);
        JSONObject jsonResult = JSON.parseObject(result);
        return jsonResult;
    }


}
